package com.epalmpay.service.shop.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.epalmpay.dto.shopuser.ShopUserInDTO;
import com.epalmpay.dto.shopuser.ShopUserOutDTO;
import com.epalmpay.dto.shopuser.ShopUserTradeDetailOutDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.ShopUser;
import com.epalmpay.mapper.MemberGradeMapper;
import com.epalmpay.mapper.ShopUserMapper;
import com.epalmpay.mapper.TradeOfflineMapper;
import com.epalmpay.service.shop.IShopUserService;

@Service("shopUserService") 
public class ShopUserServiceImpl  implements IShopUserService{
	
	@Resource  
    private ShopUserMapper shopUserMapper;
	@Resource  
	private MemberGradeMapper memberGradeMapper;
	@Resource  
	private TradeOfflineMapper tradeOfflineMapper;

	@Override
	public List<ShopUserOutDTO> getShopUserList(ShopUserInDTO shopUserInDTO, Page page) {
		Map<String,Object> param=new HashMap<String,Object>();
		
		param.put("shopId", shopUserInDTO.getShopId());
		param.put("gradeId", shopUserInDTO.getGradeId());
		param.put("joinStart", shopUserInDTO.getJoinStart());
		param.put("joinEnd", shopUserInDTO.getJoinEnd());
		param.put("keyWord", shopUserInDTO.getKeyWord());
		
		List<ShopUserOutDTO> result = new ArrayList<ShopUserOutDTO>();

		int sumCount = shopUserMapper.selectShopUserListCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>> shopUserList = shopUserMapper.selectShopUserList(param);
		
		
		for (Map<String, Object> shopUser : shopUserList) {
			ShopUserOutDTO shopUserOutDTO = new ShopUserOutDTO();
			
			shopUserOutDTO.setId((Long)(shopUser.get("id")));
			shopUserOutDTO.setCardNo((String)shopUser.get("card_no"));
			shopUserOutDTO.setMemberName((String)shopUser.get("member_name"));
			shopUserOutDTO.setMemberGradeName((String)shopUser.get("member_grade_name"));
			shopUserOutDTO.setConsumAmount((Integer)shopUser.get("consum_amount"));
			shopUserOutDTO.setConsumCount((Integer)shopUser.get("consum_count"));
			shopUserOutDTO.setJoinDate((Date)shopUser.get("last_consum_time"));
			
			result.add(shopUserOutDTO);
		}
		
		return result;
	}

	@Override
	public ShopUser findShopUserDetailById(ShopUser shopUser) {
		return shopUserMapper.findShopUserDetailById(shopUser.getId());
	}

	@Override
	public List<ShopUserTradeDetailOutDTO> queryShopUserDetailList(ShopUserInDTO shopUserInDTO) {
		return tradeOfflineMapper.queryShopUserDetailList(shopUserInDTO);
	}
	
}
